<template>
  <el-config-provider :locale="locale" :button="button">
    <router-view />
  </el-config-provider>
</template>

<script lang="ts" setup>
import { useTheme } from '_hooks'
// import zhCn from 'element-plus/lib/locale/lang/zh-cn'
import zhCN from "element-plus/dist/locale/zh-cn.mjs"
import { onBeforeMount, ref } from 'vue'
import { ElConfigProvider, ElNotification } from "element-plus"
// import fixedData from '../public/version.json'
import {
  getVersionUpdate,
  type getVersion
} from '@/api/public'

const { initTheme } = useTheme()
initTheme()
/** 将 Element Plus 的语言设置为中文 */
const locale = ref(zhCN)
const button = ref({ autoInsertSpace: true })

onBeforeMount(() => {
  // 检测版本更新
  const { MODE } = import.meta.env;
  if (MODE !== "development") {
    versionUpdate()
  }
})

// 版本更新检测
const versionUpdate = async () => {
  const res = await getVersionUpdate();
  const { version } = res as getVersion;
  if (!localStorage.getItem('dashan-version')) {
    if (res) { 
      localStorage.setItem('dashan-version', JSON.stringify(res))
    }
  } else {
    if (res) {
      const versionString = localStorage.getItem('dashan-version') as string
      const localVersion = JSON.parse(versionString)
      if (localVersion.version !== version) {
        ElNotification({
          title: "新版本提示",
          message: "检测到新版本，系统将会在5秒后为您自动更新。",
          type: "success",
          duration: 5000
        });
        setTimeout(() => {
          window.location.reload();
          setTimeout(() => {
            localStorage.setItem('dashan-version', JSON.stringify(res))
          }, 10)
        }, 5000);
      }
    }
  }
}

</script>
